/**
 *  安装申请的配置列表修改时，将其重新赋值，1. 删除原有配置列表   2.插入新的配置列表
 *  created by Harry 2017-01-09 20:54:43
 */
public  class InstallationApplyHandler {
    /*
    implements Triggers.handler
    public void handle(){
        List<InstallationApply__c> triggerNew = (List<InstallationApply__c>)Trigger.new;
        Map<Id,InstallationApply__c> triggerOldMap = (Map<Id,InstallationApply__c>)Trigger.oldMap;
        Set<Id> iAIdSet = new Set<Id>();
        Set<Id> oldIAIdSet = new Set<Id>();
        Set<Id> cLIdSet = new Set<Id>();
        Map<Id,Id> idMap = new Map<Id,Id>();
        for(InstallationApply__c ia : triggerNew){
            //过滤掉[配置清单]字段没有发生改变的记录
            if(ia.Instrument__c != triggerOldMap.get(ia.Id).Instrument__c){
                iAIdSet.add(ia.Id);
                cLIdSet.add(ia.Instrument__c);
                oldIAIdSet.add(triggerOldMap.get(ia.Id).Id);
                idMap.put(ia.Id, ia.Instrument__c);
            }
        }
        Map<Id,InstallationApply__c> filterMap = null;
        Map<Id,Product__c> filterMap2 = null;
        Map<Id,InstallationApply__c> filterMap3 = null;
        //filterMap为查询出来的“更新后的”[安装申请]对象集
        if(iAIdSet.size()>0){
            filterMap = new Map<Id,InstallationApply__c>([
                    SELECT Id,
                    (SELECT Name,Remark__c,IsStandard__c,Amount__c,Configuration__c,OrderMethod__c FROM InstallApproval__r)
                    FROM InstallationApply__c WHERE Id IN : iAIdSet]);
        }
        //filterMap2为查询出来的[配置清单]对象集
        if(cLIdSet.size()>0){
            filterMap2 = new Map<Id,Product__c>([
                SELECT Id,
                (SELECT Name,IsStandard__c,Amount__c,Remark__c,Configuration__c,OrderMethod__c FROM gSxO__r)
                FROM Product__c WHERE Id IN : cLIdSet]);
        }
        //filterMap3为查询出来的“旧的”[安装申请]对象集
        if(oldIAIdSet.size()>0){
            filterMap3 = new Map<Id,InstallationApply__c>([
                SELECT Id,
                (SELECT Name,Remark__c,IsStandard__c,Amount__c,Configuration__c,OrderMethod__c FROM InstallApproval__r)
                FROM InstallationApply__c WHERE Id IN : oldIAIdSet]);
        }
        if(null!=filterMap){
            populateDetail(filterMap,filterMap2,filterMap3,idMap,triggerOldMap);
        }
        

    }
     private void populateDetail(Map<Id,InstallationApply__c> filterMap,Map<Id,Product__c> filterMap2,Map<Id,InstallationApply__c> filterMap3,Map<Id,Id> idMap,Map<Id,InstallationApply__c> triggerOldMap){
        //待删除的安装申请队列
        List<InstallApprovalDetail__c> preDeleteIADList = new List<InstallApprovalDetail__c>();
        //待插入的安装申请队列
        List<InstallApprovalDetail__c> preInsertIADList = new List<InstallApprovalDetail__c>();
        for(Id id : filterMap.keySet()){
            InstallationApply__c ia = filterMap.get(id);
            InstallationApply__c oldIA = filterMap3.get(triggerOldMap.get(id).Id);
            List<InstallApprovalDetail__c> oldIADList = oldIA.InstallApproval__r;
            preDeleteIADList.addAll(oldIADList);
            Product__c CL = filterMap2.get(idMap.get(id));
            List<ConfigurationListDetail__c> CLDList = CL.gSxO__r;
            for(ConfigurationListDetail__c CLD : CLDList){
                InstallApprovalDetail__c newIA = new InstallApprovalDetail__c();
                newIA.Remark__c = CLD.Remark__c;
                newIA.IsStandard__c = CLD.IsStandard__c;
                newIA.Amount__c = CLD.Amount__c;
                newIA.Name = CLD.Name;
                newIA.FromConfiguration__c = true;
                newIA.InstallApproval__c = id;
                newIA.OrderMethod__c = CLD.OrderMethod__c;
                newIA.Configuration__c = CLD.Configuration__c;
                preInsertIADList.add(newIA);
            }
        }
        delete preDeleteIADList;
        insert preInsertIADList;
    }

    */
    //安装申请关联工单的时候 如果是补单的安装申请 保修条款返写到仪器上
    public  static void backToEquipment(Map<Id, InstallationApply__c> newMap ,Map<Id,InstallationApply__c> oldMap ){

        //
        Map<String,String > emap = new Map<String,String >();
        // Set<Id> accsets  = new Set<Id>();
        // Set<String > enos   = new Set<String>();
        Map<Id,Equipment__c> inaRelateEquipMap   = new Map<Id,Equipment__c >();
        Map<Id,WorkOrder__c> woMap = new Map<Id,WorkOrder__c>();
        //Set<Id> pids   = new Set<Id>();
        for ( InstallationApply__c ina : newMap.values()  ) {
            InstallationApply__c old  = oldMap.get(ina.Id);

            if(ina.SupplementOrNot__c == '是' && ina.RelatedWorkOrder__c != null  && ina.Status__c == '已批准' && old.Status__c != '已批准'  ){

                
                woMap.put(ina.RelatedWorkOrder__c, new WorkOrder__c());

               // String key = ina.Hospital__c +ina.FuselageNumber__c + ina.Instrument__c;
                emap.put(ina.RelatedWorkOrder__c, ina.Id );
                if(ina.ReturnedMachine__c != null )inaRelateEquipMap.put(ina.ReturnedMachine__c, new Equipment__c());


            }
        }



        if(emap.isEmpty())return;

        //退换仪器
        for (Equipment__c eq : [select Id ,
               //安装申请关联仪器的状态
            Type__c,
            //关联仪器的保修条款
            IMEINumber__c ,
            ApplicationTraining__c,
            EquipmentCheck__c,
            EquipmentCheckGoods__c,
            SpecialRequirement__c,
            WarrantyPeriod__c,
            ChargingStandard__c,
            ResponseHours__c,
            Period__c,
            CheckPeriod__c,
            RapidWear__c,   
            CommonAccessories__c,
            Warranty__c,
            OpponentSpecialTeam__c,
            EquipmentType__c,
            ExpiryDate__c,
            EnterWay__c,
            ISE__c
            From Equipment__c where Id in: inaRelateEquipMap.keySet()]) {
            inaRelateEquipMap.put(eq.Id , eq);
            
        }
        //关联工单
        for (WorkOrder__c wo  : [select Id ,InstallData__c,IMEINumber__c,Technician__r.Name,
            ManufactureInfo__c,WhetherToMakeASingle__c,WhetherStandalone__c,CombinationForm_Ins__c,
            Case__r.InstallationApply__r.NEWDealer__c,Account__c,Software_Version__c,InstrumentVersion__c,
            Terminal__c,UserDepartmen__c,UserDepartmentContact__c,Technician__c,AssociatedEquipment__c,
            AssociatedEquipment__r.ExpiryDate__c,ISE__c,Case__c ,
            Case__r.TechnicalServicesApplicationNum__r.OpponentDept__c,
             //双联机
            (select  Id,MainInstrument__c,InstallDate__c,RelatedInstruments__c ,Code__c  from LineInsWorkOrder__r )
            from WorkOrder__c where Id in: woMap.keySet()]) {
            woMap.put(wo.Id , wo);
        }


        Map<String,Equipment__c> em = new Map<String,Equipment__c>();
        //Account__c in:accsets and Product__c in:pids and  IMEINumber__c in: enos
        for (Equipment__c e  : [select Id,IMEINumber__c ,OpponentStatus__c,Product__c,Account__c,RelatedWorkOrders__c   from Equipment__c where RelatedWorkOrders__c =:woMap.keySet() ]) {
            //em.put(e.Account__c+e.IMEINumber__c+e.Product__c,e);
            em.put(e.RelatedWorkOrders__c , e);

            //e.RelatedWorkOrders__c 
        }

        Map<Id,Equipment__c> eList = new Map<Id,Equipment__c>();

        Map<Id,WorkOrder__c> woList = new Map<Id,WorkOrder__c>();
        Map<Id,Case> casMap =new Map<Id,Case>();
        for (String key  :  emap.keySet()) {
            InstallationApply__c ia =  newMap.get(emap.get(key));
            if(em.containsKey(key)){

                Equipment__c eq = em.get(ia.RelatedWorkOrder__c);
                Boolean isOldEnsure = false;
                Equipment__c reEq =  inaRelateEquipMap.get(ia.ReturnedMachine__c) ;
                WorkOrder__c wor = woMap.get(ia.RelatedWorkOrder__c);

                if(ia.InstallationType__c  == '新装机' ||  ia.InstallationType__c  == '加装' || ia.InstallationType__c  == '双机联机'){

                   // eq = new Equipment__c();
                    // 加装 生成一个新资产 将新的资产 关联到 原来的仪器

                }else if( ia.InstallationType__c  == '升级'){
                    
                    // Equipment__c e1 =  new Equipment__c (Id= ia.ReturnedMachine__c );
                    // //e1.Product__c = wor.ManufactureInfo__c;
                    // e1.ExpiryDate__c = reEq.ExpiryDate__c;
                    // e1.Type__c = '已升级';
                    // e1.FromFlow__c = -1 * Math.round(Math.random() * 10) ;
                    // //e1.RelatedWorkOrders__c = wor.Id;
                    //eList.put(e1.Id , e1);
                    isOldEnsure = true ;

                }else if(ia.InstallationType__c  == '同客户更换同型号不同机身号仪器' ){
                                    
                    // eq.Product__c = wor.ManufactureInfo__c;
                    // eq.IMEINumber__c = wor.IMEINumber__c;
                    //eq.ExpiryDate__c = reEq.ExpiryDate__c;
                     isOldEnsure = true ;

                }else if(ia.InstallationType__c  == '同机身号仪器更换客户'){
                    if(reEq.IMEINumber__c != wor.IMEINumber__c )throw new DmlException('同仪器换安装客户，机身号必须等于被退机仪器的机身号! ');
                    isOldEnsure = true ;
                }

                eq.IMEINumber__c=wor.IMEINumber__c;
                if(eq.OpponentStatus__c == null )eq.OpponentStatus__c = '安装未使用';
                // eq.OpponentStatus__c = wor.InstrumentUsed__c == null ? null : (wor.InstrumentUsed__c == '是' ? '在用':'未用');
                //if(wor.WhetherToMakeASingle__c != '是')eq.EndDate__c=Date.valueOf(wor.Install_End_Datetime__c);
                if(eq.EndDate__c == null )eq.EndDate__c=wor.InstallData__c;
                eq.Product__c=wor.ManufactureInfo__c;
                eq.Number__c=1;
                eq.WhetherToMakeASingle__c = wor.WhetherToMakeASingle__c;
                eq.FromWorkOrder__c = true;
                eq.WhetherStandalone__c = wor.WhetherStandalone__c;
                eq.CombinationForm_Ins__c = wor.CombinationForm_Ins__c;
                eq.Supplier__c = wor.Case__r.InstallationApply__r.NEWDealer__c;
                eq.Account__c=wor.Account__c;///客户
                eq.RelatedWorkOrders__c = wor.Id;//关联工单
                eq.Software_Version__c = wor.Software_Version__c;//软件版本
                eq.InstrumentVersion__c = wor.InstrumentVersion__c;//仪器版本
                eq.Terminal__c = wor.Terminal__c; //端子

                //eq.OpponentDept__c = wor.UserDepartmen__c;//科室

                //if(ia != null )eq.OpponentDept__c  = ia.OpponentDept__c;
                //if(ia == null)eq.OpponentDept__c  = wor.Case__r.TechnicalServicesApplicationNum__r.OpponentDept__c ; 

                eq.DepartmentHeads__c  = wor.UserDepartmentContact__c;//科室负责人
                eq.Installer__c = wor.Technician__r.Name ;//安装人员
                eq.ISE__c  = wor.ISE__c;


                if(ia != null )eq.ContractInstruments__c = ia.ContractEquipment__c;
                if(ia != null )eq.EnterWay__c = ia.EnterWay__c ;
                if(ia != null )eq.RelatedInstallationApplication__c = ia.Id ;
                if(ia != null )eq.ApplicationTraining__c = isOldEnsure  ? reEq.ApplicationTraining__c :ia.ApplicationTraining__c;//应用培训
                if(ia != null )eq.EquipmentCheck__c = isOldEnsure  ? reEq.EquipmentCheck__c : ia.EquipmentCheck__c;//仪器校准
                if(ia != null )eq.EquipmentCheckGoods__c =isOldEnsure  ? reEq.EquipmentCheckGoods__c : ia.EquipmentCheckGoods__c;//仪器校准品
                if(ia != null )eq.SpecialRequirement__c = isOldEnsure  ? reEq.SpecialRequirement__c : ia.SpecialRequirement__c;//特殊需求
                if(ia != null )eq.WarrantyPeriod__c =isOldEnsure  ? reEq.WarrantyPeriod__c :  ia.WarrantyPeriod__c;//保修年限
                if(ia != null )eq.ChargingStandard__c  = isOldEnsure  ? reEq.ChargingStandard__c :  ia.ChargingStandard__c;//保修期满后配件收费标准
                try{
                    
                     if(ia != null )eq.ExpiryDate__c = isOldEnsure ?reEq.ExpiryDate__c:  eq.EndDate__c.addYears(Integer.valueOf(eq.WarrantyPeriod__c) );
                     //if(ia == null )eq.ExpiryDate__c =eq.EndDate__c.addYears(1);

                 }catch(Exception error ){}
                

                if(ia != null )eq.ResponseHours__c = isOldEnsure  ? reEq.ResponseHours__c : ia.ResponseHours__c;//响应时间
                if(ia != null )eq.Period__c = isOldEnsure  ? reEq.Period__c :ia.Period__c;//维护保养
                if(ia != null )eq.CheckPeriod__c =isOldEnsure  ? reEq.CheckPeriod__c :  ia.CheckPeriod__c;//仪器校准周期
                if(ia != null )eq.RapidWear__c = isOldEnsure  ? reEq.RapidWear__c : ia.RapidWear__c;//易损件    
                if(ia != null )eq.CommonAccessories__c  =isOldEnsure  ? reEq.CommonAccessories__c : ia.CommonAccessories__c;//常用配件
                if(ia != null )eq.Warranty__c = isOldEnsure  ? reEq.Warranty__c : ia.Warranty__c;//维保项目
                if(ia != null )eq.OpponentSpecialTeam__c = isOldEnsure  ? reEq.OpponentSpecialTeam__c : ia.OpponentSpecialTeam__c;
                if(ia != null )eq.EquipmentType__c = isOldEnsure  ? reEq.EquipmentType__c : ia.EquipmentType__c;
                
                if(ia != null && ia.ContractEquipment__c != null )ContractCalcEquipmentUtil.installBackToContract(ia.ContractEquipment__c);

                System.debug('Cbl_WorkOrderToassert==>');
                 if( ia.InstallationType__c  == '升级'  || ia.InstallationType__c  == '加装' || ia.InstallationType__c  == '同机身号仪器更换客户' || ia.InstallationType__c  == '同客户更换同型号不同机身号仪器' ){

                    Equipment__c e =  new Equipment__c(Id=ia.ReturnedMachine__c,Instrument__c=eq.Id,OpponentStatus__c = '使用后停用');
                    if(ia.InstallationType__c  == '加装')e.Type__c = '已被加装';
                    if(ia.InstallationType__c  == '升级')e.Type__c = '已升级';

                    eList.put(e.Id, e );
                    
                 } 
                 if(ia.InstallationType__c  == '双机联机'){

                    for (LineInstrument__c lin : wor.LineInsWorkOrder__r  ) {
                        eList.put(lin.RelatedInstruments__c, new Equipment__c(Id=lin.RelatedInstruments__c,Instrument__c=eq.Id,Type__c = '已联机' ));
                    }
                    
                 } 

                eList.put(eq.Id , eq);

                //工单关联安装申请
                 if(ia != null && !woList.containsKey(ia.RelatedWorkOrder__c) )woList.put(ia.RelatedWorkOrder__c, new WorkOrder__c(Id=ia.RelatedWorkOrder__c,WorkOrderInstallApplication__c=ia.Id ));
            
                //个案关联安装申请
                 if(ia != null && !casMap.containsKey(wor.Case__c) )casMap.put(wor.Case__c, new Case(Id=wor.Case__c,InstallationApply__c=ia.Id ));

                
            }else{

                ia.addError('补单安装申请没有找到对应的仪器资产!'+key);

                return;
            } 
        }

        if(!eList.isEmpty())update eList.values();
        if(!woList.isEmpty() )update woList.values();
        if(!casMap.isEmpty())update casMap.values();



    }

    
    /**
        赋值
     */
    /*
    private void populateDetail(Map<Id,InstallationApply__c> filterMap,Map<Id,ConfigurationList__c> filterMap2,Map<Id,InstallationApply__c> filterMap3,Map<Id,Id> idMap,Map<Id,InstallationApply__c> triggerOldMap){
        //待删除的安装申请队列
        List<InstallApprovalDetail__c> preDeleteIADList = new List<InstallApprovalDetail__c>();
        //待插入的安装申请队列
        List<InstallApprovalDetail__c> preInsertIADList = new List<InstallApprovalDetail__c>();
        for(Id id : filterMap.keySet()){
            InstallationApply__c ia = filterMap.get(id);
            InstallationApply__c oldIA = filterMap3.get(triggerOldMap.get(id).Id);
            List<InstallApprovalDetail__c> oldIADList = oldIA.InstallApproval__r;
            preDeleteIADList.addAll(oldIADList);
            ConfigurationList__c CL = filterMap2.get(idMap.get(id));
            List<ConfigurationListDetail__c> CLDList = CL.ConfigurationListMD__r;
            for(ConfigurationListDetail__c CLD : CLDList){
                InstallApprovalDetail__c newIA = new InstallApprovalDetail__c();
                newIA.ShipNo__c = CLD.ShipNo__c;
                newIA.Remark__c = CLD.Remark__c;
                newIA.IsStandard__c = CLD.IsStandard__c;
                newIA.Amount__c = CLD.Amount__c;
                newIA.Name = CLD.Name;
                newIA.InstallApproval__c = id;
                preInsertIADList.add(newIA);
            }
        }
        delete preDeleteIADList;
        insert preInsertIADList;
    }
    /*public static void handle(){
        
         List<InstallationApply__c> triggerNew = (List<InstallationApply__c>)Trigger.new;
    	 Set<Id> iAIdSet = new Set<Id>();
   		 Set<Id> cLIdSet = new Set<Id>();//产品
        
   		 Map<Id,Id> idMap = new Map<Id,Id>();
    	for(InstallationApply__c ia : triggerNew){
    		iAIdSet.add(ia.Id);
    		cLIdSet.add(ia.Instrument__c);
    		idMap.put(ia.Id, ia.Instrument__c);
    	}
   		Map<Id,InstallationApply__c> filterMap = null;
    	Map<Id,Product__c> filterMap2 = null;
    //filterMap为查询出来的“更新后的”[安装申请]对象集
    	if(iAIdSet.size()>0){
    		filterMap = new Map<Id,InstallationApply__c>([
    			SELECT Id,
    			(SELECT Name,Remark__c,ShipNo__c,IsStandard__c,Amount__c FROM InstallApproval__r)
    			FROM InstallationApply__c WHERE Id IN : iAIdSet]);
    	}
    //filterMap2为查询出来的[配置清单]对象集
    	if(cLIdSet.size()>0){
    		filterMap2 = new Map<Id,Product__c>([
    			SELECT Id,
    			(SELECT Name,IsStandard__c,Amount__c,ShipNo__c,Remark__c FROM gSxO__r)
    			FROM Product__c WHERE Id IN : cLIdSet]);
    	}
    	if(null!=filterMap){
    		InstallationApplyHandler.populateDetail(filterMap,filterMap2,idMap);
    	}
        
              
    }
    private static void populateDetail(Map<Id,InstallationApply__c> filterMap,Map<Id,Product__c> filterMap2,Map<Id,Id> idMap){
    //待插入的安装申请队列
    	List<InstallApprovalDetail__c> preInsertIADList = new List<InstallApprovalDetail__c>();
   		for(Id id : filterMap.keySet()){
    		InstallationApply__c ia = filterMap.get(id);
    		Product__c CL = filterMap2.get(idMap.get(id));
    		if(null!=CL){
    		List<ConfigurationListDetail__c> CLDList = CL.gSxO__r;
    			for(ConfigurationListDetail__c CLD : CLDList){
    				InstallApprovalDetail__c newIA = new InstallApprovalDetail__c();
    				newIA.ShipNo__c = CLD.ShipNo__c;
    				newIA.Remark__c = CLD.Remark__c;
    				newIA.IsStandard__c = CLD.IsStandard__c;
    				newIA.Amount__c = CLD.Amount__c;
    				newIA.Name = CLD.Name;
    				newIA.InstallApproval__c = id;
    				preInsertIADList.add(newIA);
    	}
         System.debug( '产品配置明细' + CLDList);
    	}

    	}
    	 System.debug(preInsertIADList);
    	insert preInsertIADList;
    	}
       
     public static void updatehandle(){
        List<InstallationApply__c> triggerNew = (List<InstallationApply__c>)Trigger.new;
        Map<Id,InstallationApply__c> triggerOldMap = (Map<Id,InstallationApply__c>)Trigger.oldMap;
        Set<Id> iAIdSet = new Set<Id>();
        Set<Id> oldIAIdSet = new Set<Id>();
        Set<Id> cLIdSet = new Set<Id>();//产品
        Map<Id,Id> idMap = new Map<Id,Id>();
        for(InstallationApply__c ia : triggerNew){
            //过滤掉[配置清单]字段没有发生改变的记录
            if(ia.ConfigurationList__c != triggerOldMap.get(ia.Id).Instrument__c){
                iAIdSet.add(ia.Id);
                cLIdSet.add(ia.Instrument__c);
                oldIAIdSet.add(triggerOldMap.get(ia.Id).Id);
                idMap.put(ia.Id, ia.Instrument__c);
            }
        }
        Map<Id,InstallationApply__c> filterMap = null;
        Map<Id,Product__c> filterMap2 = null;
        Map<Id,InstallationApply__c> filterMap3 = null;
        //filterMap为查询出来的“更新后的”[安装申请]对象集
        if(iAIdSet.size()>0){
            filterMap = new Map<Id,InstallationApply__c>([
                    SELECT Id,Instrument__c,
                    (SELECT Name,Remark__c,ShipNo__c,IsStandard__c,Amount__c FROM InstallApproval__r)
                    FROM InstallationApply__c WHERE Id IN : iAIdSet]);
        }
        //filterMap2为查询出来的[配置清单]对象集
        if(cLIdSet.size()>0){
            filterMap2 = new Map<Id,Product__c>([
                SELECT Id,
                (SELECT Name,IsStandard__c,Amount__c,ShipNo__c,Remark__c FROM gSxO__r)
                FROM Product__c WHERE Id IN : cLIdSet]);
        }
        //filterMap3为查询出来的“旧的”[安装申请]对象集
        if(oldIAIdSet.size()>0){
            filterMap3 = new Map<Id,InstallationApply__c>([
                SELECT Id,Instrument__c,
                (SELECT Name,Remark__c,ShipNo__c,IsStandard__c,Amount__c FROM InstallApproval__r)
                FROM InstallationApply__c WHERE Id IN : oldIAIdSet]);
        }
        if(null!=filterMap){
            InstallationApplyHandler.populateDetail1(filterMap,filterMap2,filterMap3,idMap,triggerOldMap);
        }

    }

       
    private static void populateDetail1(Map<Id,InstallationApply__c> filterMap,Map<Id,Product__c> filterMap2,Map<Id,InstallationApply__c> filterMap3,Map<Id,Id> idMap,Map<Id,InstallationApply__c> triggerOldMap){
       
        
        //待删除的安装申请队列
        List<InstallApprovalDetail__c> preDeleteIADList = new List<InstallApprovalDetail__c>();
        //待插入的安装申请队列
        List<InstallApprovalDetail__c> preInsertIADList = new List<InstallApprovalDetail__c>();
        for(Id id : filterMap.keySet()){
            

            
            InstallationApply__c ia = filterMap.get(id);
            InstallationApply__c oldIA = filterMap3.get(triggerOldMap.get(id).Id);
            
            if(ia.Instrument__c != oldIA.Instrument__c){
            
             List<InstallApprovalDetail__c> oldIADList = oldIA.InstallApproval__r;
            preDeleteIADList.addAll(oldIADList);
            Product__c CL = filterMap2.get(idMap.get(id));
            List<ConfigurationListDetail__c> CLDList = CL.gSxO__r;
            for(ConfigurationListDetail__c CLD : CLDList){
                InstallApprovalDetail__c newIA = new InstallApprovalDetail__c();
                newIA.ShipNo__c = CLD.ShipNo__c;
                newIA.Remark__c = CLD.Remark__c;
                newIA.IsStandard__c = CLD.IsStandard__c;
                newIA.Amount__c = CLD.Amount__c;
                newIA.Name = CLD.Name;
                newIA.InstallApproval__c = id;
                preInsertIADList.add(newIA);
            }
            
            }
           
        }
        System.debug('删除原来的' + preInsertIADList);
       if(preDeleteIADList.size() > 0) delete preDeleteIADList;
       if(preInsertIADList.size() > 0) insert preInsertIADList;
    }
    */
   /* public void handle(){
        List<InstallationApply__c> triggerNew = (List<InstallationApply__c>)Trigger.new;
        Map<Id,InstallationApply__c> triggerOldMap = (Map<Id,InstallationApply__c>)Trigger.oldMap;
        Set<Id> iAIdSet = new Set<Id>();
        Set<Id> oldIAIdSet = new Set<Id>();
        Set<Id> cLIdSet = new Set<Id>();
        Map<Id,Id> idMap = new Map<Id,Id>();
        for(InstallationApply__c ia : triggerNew){
            //过滤掉[配置清单]字段没有发生改变的记录
            if(ia.ConfigurationList__c != triggerOldMap.get(ia.Id).ConfigurationList__c){
                iAIdSet.add(ia.Id);
                cLIdSet.add(ia.ConfigurationList__c);
                oldIAIdSet.add(triggerOldMap.get(ia.Id).Id);
                idMap.put(ia.Id, ia.ConfigurationList__c);
            }
        }
        Map<Id,InstallationApply__c> filterMap = null;
        Map<Id,ConfigurationList__c> filterMap2 = null;
        Map<Id,InstallationApply__c> filterMap3 = null;
        //filterMap为查询出来的“更新后的”[安装申请]对象集
        if(iAIdSet.size()>0){
            filterMap = new Map<Id,InstallationApply__c>([
                    SELECT Id,
                    (SELECT Name,Remark__c,ShipNo__c,IsStandard__c,Amount__c FROM InstallApproval__r)
                    FROM InstallationApply__c WHERE Id IN : iAIdSet]);
        }
        //filterMap2为查询出来的[配置清单]对象集
        if(cLIdSet.size()>0){
            filterMap2 = new Map<Id,ConfigurationList__c>([
                SELECT Id,
                (SELECT Name,IsStandard__c,Amount__c,ShipNo__c,Remark__c FROM ConfigurationListMD__r)
                FROM ConfigurationList__c WHERE Id IN : cLIdSet]);
        }
        //filterMap3为查询出来的“旧的”[安装申请]对象集
        if(oldIAIdSet.size()>0){
            filterMap3 = new Map<Id,InstallationApply__c>([
                SELECT Id,
                (SELECT Name,Remark__c,ShipNo__c,IsStandard__c,Amount__c FROM InstallApproval__r)
                FROM InstallationApply__c WHERE Id IN : oldIAIdSet]);
        }
        if(null!=filterMap){
            populateDetail(filterMap,filterMap2,filterMap3,idMap,triggerOldMap);
        }

    }
    */
    /**
        赋值
     */
    /*
    private void populateDetail(Map<Id,InstallationApply__c> filterMap,Map<Id,ConfigurationList__c> filterMap2,Map<Id,InstallationApply__c> filterMap3,Map<Id,Id> idMap,Map<Id,InstallationApply__c> triggerOldMap){
        //待删除的安装申请队列
        List<InstallApprovalDetail__c> preDeleteIADList = new List<InstallApprovalDetail__c>();
        //待插入的安装申请队列
        List<InstallApprovalDetail__c> preInsertIADList = new List<InstallApprovalDetail__c>();
        for(Id id : filterMap.keySet()){
            InstallationApply__c ia = filterMap.get(id);
            InstallationApply__c oldIA = filterMap3.get(triggerOldMap.get(id).Id);
            List<InstallApprovalDetail__c> oldIADList = oldIA.InstallApproval__r;
            preDeleteIADList.addAll(oldIADList);
            ConfigurationList__c CL = filterMap2.get(idMap.get(id));
            List<ConfigurationListDetail__c> CLDList = CL.ConfigurationListMD__r;
            for(ConfigurationListDetail__c CLD : CLDList){
                InstallApprovalDetail__c newIA = new InstallApprovalDetail__c();
                newIA.ShipNo__c = CLD.ShipNo__c;
                newIA.Remark__c = CLD.Remark__c;
                newIA.IsStandard__c = CLD.IsStandard__c;
                newIA.Amount__c = CLD.Amount__c;
                newIA.Name = CLD.Name;
                newIA.InstallApproval__c = id;
                preInsertIADList.add(newIA);
            }
        }
        delete preDeleteIADList;
        insert preInsertIADList;
    }
    */
}